package servlet;
import bean.Login;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.*;

@WebServlet(name = "adminUpdate", urlPatterns = "/admin-Update")
public class adminUpdate extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
    String hotelID;
        HttpSession session=request.getSession(true);
        Login login=null;
        try {
            login= (Login) session.getAttribute("ad_login");
        }catch (Exception e){}
        hotelID=login.getLoginID();
        String RoomName=request.getParameter("RoomName");
        String RoomType=request.getParameter("RoomType");
        String Price=request.getParameter("Price");
        String Roomcondi=request.getParameter("RoomCondi");
        if (Roomcondi.equals("未预定")){
            Roomcondi = "1";
        }else if (Roomcondi.equals("已预定")) {
            Roomcondi = "0";
        }
        String sql = "update rooms set RoomType='"+RoomType+"',Price='"+Price+"',RoomCondi='"+Roomcondi+"' where RoomName ='"+RoomName+"'";
        try {
            PreparedStatement statement;
            String driver="org.mariadb.jdbc.Driver";
            String url="jdbc:mariadb://localhost:3306/hotel-management-system";
//            Class.forName("org.mariadb.jdbc.Driver");
            Class.forName(driver);
            Connection con=null;
            con= DriverManager.getConnection(url,"root","123456");
            statement=con.prepareStatement(sql);
//            int result =
                    statement.executeUpdate(sql);
//            if (result>0){
//                System.out.println("更新成功！");
//            }
            con.close();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        response.sendRedirect("admin-equry.jsp?hotelID=\""+hotelID+"\"");

    }
}
